//
//  HomePageListVideoCell.m
//  ShowYork
//
//  Created by hxh on 2020/10/1.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "HomePageListVideoCell.h"
#import "HomePageContentModel.h"
@interface HomePageListVideoCell ()
@property (nonatomic,strong)UIButton *allBtn;
@property (nonatomic,strong)UILabel *contentLb;
@property (nonatomic,strong)UIImageView *videoIcon;
@property (nonatomic,strong)UIImageView *videoPic;
@property (nonatomic,strong)HomePageItemModel *model;

@property (nonatomic,strong)UIView *coverView;
@end

@implementation HomePageListVideoCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    

    self.backgroundColor = [UIColor whiteColor];
    
    self.contentLb = [[UILabel alloc]init];;
    self.contentLb.text = @"愿你们合上笔盖的刹那 有着战士收刀入鞘的骄傲";
    self.contentLb.textColor = C51;
    self.contentLb.numberOfLines = 5;
    self.contentLb.font = font(16);
    [self.contentView addSubview:self.contentLb];
    
    self.allBtn = [[UIButton alloc]init];
    [self.allBtn setTitle:@"全文" forState:UIControlStateNormal];
    [self.allBtn setTitleColor:C51 forState:UIControlStateNormal];
    self.allBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    self.allBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.allBtn addTarget:self action:@selector(allMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.allBtn];
    
    self.videoPic = [[UIImageView alloc]init];
    self.videoPic.contentMode = UIViewContentModeScaleAspectFill;
    self.videoPic.layer.masksToBounds = YES;
    self.videoPic.backgroundColor = RGBA(240, 240, 240, 1);
    [self.contentView addSubview:self.videoPic];

    self.coverView = [[UIView alloc]init];
    self.coverView.backgroundColor = RGBA(0, 0, 0, 0.1);
    [self.contentView addSubview:self.coverView];
    
    self.videoIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"social_video"]];
    self.videoIcon.highlightedImage = [UIImage imageNamed:@"social_video"];
//    [self.videoBtn setImage:[UIImage imageNamed:@"social_video"] forState:UIControlStateNormal];
//    [self.videoBtn setImage:[UIImage imageNamed:@"social_video"] forState:UIControlStateHighlighted];
//    [self.videoBtn addTarget:self action:@selector(videoMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.videoIcon];

    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-16);
    }];
    [self.allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(16);
        make.top.equalTo(self.contentLb.mas_bottom).offset(4);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    [self.videoPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.allBtn.mas_bottom).offset(16);
        make.left.equalTo(self.contentView).offset(16);
        make.size.mas_equalTo(CGSizeZero);
    }];
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.videoPic);
    }];
    
    [self.videoIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.videoPic);
    }];
    
}


- (void)allMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(videoAllContent)]) {
        [self.delegate videoAllContent];
    }
}

- (void)bindViewModel:(HomePageItemModel *)viewModel{
    self.model = viewModel;
    self.allBtn.hidden = YES;
    __block HomePageContentModel *item = nil;
    [viewModel.contents enumerateObjectsUsingBlock:^(HomePageContentModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 图片
        if (obj.seq.intValue == 1) {
            item = obj;
            *stop = YES;
        }
    }];
    
    self.contentLb.text = viewModel.contentText;

    if (viewModel.contentText.length == 0) {
        self.allBtn.hidden = YES;
        [self.allBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeZero);
            make.top.mas_equalTo(self.contentLb.mas_bottom);
        }];
    }else{
        // 处理是否大于5行
        NSInteger index = [self rowsOfString:viewModel.contentText withFont:font(16) withWidth:kScreenW - 32];
        self.allBtn.hidden = NO;
        if (index >= 5) {
            [self.allBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentLb.mas_bottom).offset(4);
                make.size.mas_equalTo(CGSizeMake(100, 20));
            }];
        }else{
            self.allBtn.hidden = YES;
            [self.allBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeZero);
                make.top.mas_equalTo(self.contentLb.mas_bottom);
            }];
        }
    }
    
    [self.videoPic mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(viewModel.imageHeight/4*3, viewModel.imageHeight));
    }];

    [self.videoPic sd_setImageWithURL:[NSURL URLWithString:item.postUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
    }];
  
}

-(NSInteger)rowsOfString:(NSString *)text withFont:(UIFont *)font withWidth:(CGFloat)width{
    if (!text || text.length == 0) {
        return 0;
    }
    CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge  id)myFont range:NSMakeRange(0, attStr.length)];
    CFRelease(myFont);
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,width,MAXFLOAT));
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    NSArray *lines = ( NSArray *)CTFrameGetLines(frame);
    return lines.count;
}

- (void)videoMethod{
    
}

@end
